package cn.com.gridinfo.sooc.core.expand;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;
import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;

import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.common.Const;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.activity.CallNameActivity;
import cn.com.gridinfo.sooc.core.expand.feedback.activity.FeedbackActivity;
import cn.com.gridinfo.sooc.core.expand.score.activity.MyCountsActivity;
import cn.com.gridinfo.sooc.core.expand.signin.activity.SigninActivity;
import cn.com.gridinfo.sooc.core.login.activity.LoginActivity;
import cn.com.gridinfo.sooc.utils.IntentUtil;

/**
 * 发现界面
 *
 * @author yingchunli
 */
public class ExpandFragment extends BaseSoocFragment {

    View view;//当前界面
    @InjectView(R.id.banner_ll)
    LinearLayout bannerLl;

    public static ExpandFragment newInstance() {
        return new ExpandFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_main, null);
        ButterKnife.inject(this, view);
        //显示广告
        baidDuBannerAd(getActivity(), bannerLl);
        return view;
    }

    /**
     * 课程签到的点击事件
     */
    @OnClick(R.id.sign)
    void onClickofCourseSign() {
        //判断是否登录
        if (!Arad.preferences.getBoolean(LocalMark.IS_AUTO_LOGIN.getName(), false)) {
            IntentUtil.intentPushUp(getActivity(), LoginActivity.class, 00);
        } else {
            //跳转到签到界面
            IntentUtil.intentSlidIn(getActivity(), SigninActivity.class, 01);
        }
    }

    /**
     * 赚零钱的点击事件
     */
    @OnClick(R.id.zhuanlingqian)
    void onGetScoreClick() {
        //判断是否登录
        if (!Arad.preferences.getBoolean(LocalMark.IS_AUTO_LOGIN.getName(), false)) {
            IntentUtil.intentPushUp(getActivity(), LoginActivity.class, 00);
        } else {
            //跳转到我的积分页面
            IntentUtil.intentSlidIn(getActivity(), MyCountsActivity.class, 01);
        }
    }

    /**
     * 我要点名的点击
     */
    @OnClick(R.id.dianming)
    void onCallnameClick() {
        //判断是否登录
        if (!Arad.preferences.getBoolean(LocalMark.IS_AUTO_LOGIN.getName(), false)) {
            IntentUtil.intentPushUp(getActivity(), LoginActivity.class, 00);
        } else {
            //跳转到我要点名页面
            IntentUtil.intentSlidIn(getActivity(), CallNameActivity.class, 01);
        }
    }

    /**
     * 意见反馈的点击
     */
    @OnClick(R.id.yijian)
    void onFeedBackClick() {
        //判断是否登录
        if (!Arad.preferences.getBoolean(LocalMark.IS_AUTO_LOGIN.getName(), false)) {
            IntentUtil.intentPushUp(getActivity(), LoginActivity.class, 00);
        } else {
            //跳转到意见反馈界面
            IntentUtil.intentSlidIn(getActivity(), FeedbackActivity.class, 01);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    /**
     * 百度 banner 广告
     *
     * @param context
     * @param ll
     */
    public static void baidDuBannerAd(Context context, LinearLayout ll) {

        // 创建广告View
        String adPlaceId = Const.baidu_bannerAd_id;//重要：请填上您的广告位ID
        AdView adView = new AdView(context, adPlaceId);
        // 设置监听器
        adView.setListener(new AdViewListener() {
            public void onAdSwitch() {
                Log.d("onAdSwitch");
            }

            public void onAdShow(JSONObject info) {
                // 广告已经渲染出来
                Log.d("onAdShow " + info.toString());
            }

            public void onAdReady(AdView adView) {
                // 资源已经缓存完毕，还没有渲染出来
                Log.d("onAdReady " + adView);
            }

            public void onAdFailed(String reason) {
                //表示没有广告
                Log.d("onAdFailed " + reason);
            }

            public void onAdClick(JSONObject info) {
                Log.d("onAdClick " + info.toString());
            }
        });

        //将adView添加到父控件中(注：该父控件不一定为您的根控件，只要该控件能通过addView能添加广告视图即可)
        ll.addView(adView);
    }

}
